package com.tansuo365.test1.service.otherservice.impl;

import com.tansuo365.test1.bean.otherbean.HuiShouDetails;
import com.tansuo365.test1.bean.otherbean.HuiShouZong;
import com.tansuo365.test1.mapper.kucunmapper.ChukuZongMapper;
import com.tansuo365.test1.mapper.othermapper.HuiShouDetailsMapper;
import com.tansuo365.test1.mapper.othermapper.HuiShouZongMapper;
import com.tansuo365.test1.service.otherservice.HuishouZongService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HuishouZongServiceImpl implements HuishouZongService {

    @Resource
    private HuiShouZongMapper huiShouZongMapper;
    @Resource
    private HuiShouDetailsMapper huiShouDetailsMapper;
    @Resource
    private ChukuZongMapper chukuZongMapper;

    @Override
    public String getTodayMaxHuiShouDanNumber() {
        return huiShouZongMapper.getTodayMaxHuiShouDanNumber();
    }

    @Override
    public List<HuiShouZong> listHuiShouZong(HuiShouZong huiShouZong) {
        return huiShouZongMapper.listHuiShouZong(huiShouZong);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return huiShouZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return huiShouZongMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public int insertSelective(HuiShouZong huiShouZong) {
        return huiShouZongMapper.insertSelective(huiShouZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(HuiShouZong huiShouZong, List<HuiShouDetails> huishouDetailsList) {
        int insertCode = 0;
        insertCode = huiShouZongMapper.insertSelective(huiShouZong);
        if(!(insertCode>0)){
            return 0;
        }
        int i = 0;
        Integer zongzhangId = huiShouZong.getId();
        System.out.println("huishouZong id:"+zongzhangId);
        //保存每个回收单详细
        String duiyingchukudanhao = "";
        for(HuiShouDetails huiShouDetail : huishouDetailsList){
            //设置对应总表id
            i++;
            if(duiyingchukudanhao == ""){
                duiyingchukudanhao = huiShouDetail.getDuiyingchukudanhao();
            }
            huiShouDetail.setZongzhangid(zongzhangId);
            huiShouDetailsMapper.insertSelective(huiShouDetail);
            //
        }
        if(!(i==huishouDetailsList.size() && i!=0)){
            return 0;
        }
        int i1 = chukuZongMapper.updateCandeleteToOne(duiyingchukudanhao);
        if(i1>0){
            return i;
        }else{
            return -3;
        }
    }

    @Override
    public HuiShouZong selectByPrimaryKey(Integer id) {
        return huiShouZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(HuiShouZong huiShouZong) {
        return huiShouZongMapper.updateByPrimaryKeySelective(huiShouZong);
    }
}
